package com.warom.sdg.webview.jsinterface;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.warom.sdg.callback.WebViewJsCallback;
import com.warom.sdg.util.LogUtils;
import com.warom.sdg.webview.WebViewWrapper;

/**
 * WebView JavaScript接口实现类
 * 作为JavaScript调用的入口，将请求分发到各个专门的接口处理类
 */
public class WebViewJsInterface implements WebViewJsCallback {
    private static final String TAG = "WebViewJsInterface";
    
    // 设备管理接口
    private WebViewJsDeviceInterface deviceInterface;
    
    // 监测数据接口
    private WebViewJsMonitoringInterface monitoringInterface;
    
    // 通风控制接口
    private WebViewJsVentilationInterface ventilationInterface;
    
    // 通风配置接口
    private WebViewJsVentilationConfigInterface ventilationConfigInterface;
    
    // 能耗监测接口
    private WebViewJsPowerMonitoringInterface powerMonitoringInterface;
    
    // 粮情检测接口
    private WebViewJsGrainSituationInterface grainSituationInterface;
    
    // 虫情检测接口
    private WebViewJsPestDetectionInterface pestDetectionInterface;
    
    // 其他模块接口
    private WebViewJsOtherModuleInterface otherModuleInterface;
    
    // 用户管理接口
    private WebViewJsUserManagerInterface userManagerInterface;
    
    // 操作日志服务接口
    private WebViewJsOperationLogInterface operationLogInterface;
    
    // 阈值设置接口
    private WebViewJsThresholdSettingInterface thresholdSettingInterface;
    
    // 报警分析接口
    private WebViewJsAlarmAnalysisInterface alarmAnalysisInterface;
    
    // WebView包装类
    private WebViewWrapper webViewWrapper;
    
    /**
     * 构造函数
     * @param context 上下文
     * @param webViewWrapper WebView包装类，用于回调
     */
    public WebViewJsInterface(Context context, WebViewWrapper webViewWrapper) {
        this.webViewWrapper = webViewWrapper;
        
        // 初始化各个接口处理类
        this.deviceInterface = new WebViewJsDeviceInterface(context, webViewWrapper);
        this.monitoringInterface = new WebViewJsMonitoringInterface(context, webViewWrapper);
        this.ventilationInterface = new WebViewJsVentilationInterface(context, webViewWrapper);
        // 初始化通风配置接口
        this.ventilationConfigInterface = new WebViewJsVentilationConfigInterface(context, webViewWrapper);
        // 初始化能耗监测接口
        this.powerMonitoringInterface = new WebViewJsPowerMonitoringInterface(context,webViewWrapper);
        // 初始化粮情检测接口
        this.grainSituationInterface = new WebViewJsGrainSituationInterface(context,webViewWrapper);
        // 初始化虫情检测接口
        this.pestDetectionInterface = new WebViewJsPestDetectionInterface(context, webViewWrapper);
        // 初始化其他模块接口
        this.otherModuleInterface = new WebViewJsOtherModuleInterface(context, webViewWrapper);
        // 初始化用户管理接口
        this.userManagerInterface = new WebViewJsUserManagerInterface(context,webViewWrapper);
        // 初始化操作日志接口
        this.operationLogInterface = new WebViewJsOperationLogInterface(context, webViewWrapper);
        // 初始化阈值设置接口
        this.thresholdSettingInterface = new WebViewJsThresholdSettingInterface(context, webViewWrapper);
        // 初始化报警分析接口
        this.alarmAnalysisInterface = new WebViewJsAlarmAnalysisInterface(context, webViewWrapper);
        
        LogUtils.logOperation("WebViewJsInterface", "初始化", "JavaScript接口初始化完成");
    }
    
    /**
     * 通用的JS调用接口
     * 注意：该方法需要返回一个唯一ID而不是实际结果，实际结果将通过回调传递
     * @param method 方法名
     * @param args 参数
     * @return 返回请求ID或错误信息
     */
    @JavascriptInterface
    @Override
    public String onJsCall(String method, String args, String requestId) {
        Log.d(TAG, "JS调用方法: " + method + ", 参数: " + args);
        LogUtils.logOperation("WebView", "JS调用", "方法: " + method + ", 参数: " + args);
        
        // 根据方法名调用相应的处理方法
        switch (method) {
            // 设备管理相关
            case "getDeviceList":
                deviceInterface.getDeviceList(requestId);
                break;
            case "getDeviceById":
                deviceInterface.getDeviceById(args, requestId);
                break;
            case "getDeviceTypeList":
                deviceInterface.getDeviceTypeList(args, requestId);
                break;
            case "addDevice":
                deviceInterface.addDevice(args, requestId);
                break;
            case "updateDevice":
                deviceInterface.updateDevice(args, requestId);
                break;
            case "deleteDevice":
                deviceInterface.deleteDevice(args, requestId);
                break;
            case "testDeviceConnection":
                deviceInterface.testDeviceConnection(args, requestId);
                break;
            // 用户管理相关
            case "login":
                userManagerInterface.login(args, requestId);
                break;
            // 监测数据相关
            case "getGasRealtimeData":
                monitoringInterface.getGasRealtimeData(args, requestId);
                break;
            case "getGasHistoryData":
                monitoringInterface.getGasHistoryData(args, requestId);
                break;
            // 通风控制相关
            case "getVentilationData":
                ventilationInterface.getVentilationData(args, requestId);
                break;
            case "controlVentilation":
                ventilationInterface.controlVentilation(args, requestId);
                break;
            // 通风配置相关
            case "getVentilationConfigList":
                ventilationConfigInterface.getVentilationConfigList(args, requestId);
                break;
            case "updateVentilationConfig":
                ventilationConfigInterface.updateVentilationConfig(args, requestId);
                break;
            // 能耗监测相关
            case "getPowerRealtimeData":
                powerMonitoringInterface.getPowerMonitoringDataAsync(args, requestId);
                break;
            // 粮情检测相关
            case "getGrainSituationData":
                grainSituationInterface.getGrainSituationDataAsync(args, requestId);
                break;
            case "getHistoryGrainSituationData":
                grainSituationInterface.getHistoryGrainSituationDataAsync(args, requestId);
                break;
            case "getTemperatureData":
                grainSituationInterface.getTemperatureDataAsync(args, requestId);
                break;
            case "getHumidityData":
                grainSituationInterface.getHumidityDataAsync(args, requestId);
                break;
            case "getCableData":
                grainSituationInterface.getCableDataAsync(args, requestId);
                break;
            // 虫情检测相关
            case "getPestDetectionHistory":
                pestDetectionInterface.getPestDetectionHistory(args, requestId);
                break;
            // 其他模块相关
            case "getOtherData":
                otherModuleInterface.getOtherDataAsync(args, requestId);
                break;
            case "controlOtherDevice":
                otherModuleInterface.controlOtherDeviceAsync(args, requestId);
                break;
                
            // 用户管理相关
            case "getUserList":
                userManagerInterface.getUserList(requestId);
                break;
            case "addUser":
                userManagerInterface.addUser(args, requestId);
                break;
            case "updateUser":
                userManagerInterface.updateUser(args, requestId);
                break;
            case "deleteUser":
                userManagerInterface.deleteUser(args, requestId);
                break;
                
            // 操作日志相关
            case "logOperation":
                //operationLogInterface.logOperation(args, requestId);
                break;
            case "logDeviceControl":
                operationLogInterface.logDeviceControl(args, requestId);
                break;
            case "logConfigChange":
                //operationLogInterface.logConfigChange(args, requestId);
                break;
            case "getOperationLogs":
                operationLogInterface.getOperationLogs(args, requestId);
                break;
                
            // 阈值设置相关
            case "getThresholdSettings":
                thresholdSettingInterface.getThresholdSettings(args, requestId);
                break;
            case "updateThresholdSetting":
                thresholdSettingInterface.updateThresholdSetting(args, requestId);
                break;
                
            // 报警分析相关
            case "getGrainAlarms":
                return alarmAnalysisInterface.getGrainAlarms(args);
            case "getGasAlarms":
                return alarmAnalysisInterface.getGasAlarms(args);
            case "handleAlarm":
                return alarmAnalysisInterface.handleAlarm(args);
            case "ignoreAlarm":
                return alarmAnalysisInterface.ignoreAlarm(args);
            case "getAlarmDetail":
                return alarmAnalysisInterface.getAlarmDetail(args);
            case "getAlarmStatistics":
                return alarmAnalysisInterface.getAlarmStatistics(args);
                
            // 无效方法
            default:
                return null;
        }
        
        // 返回请求ID，表示调用成功，结果将通过回调返回
        return requestId;
    }
    
    /**
     * 接收来自JS的消息
     * @param message 消息内容
     */
    @JavascriptInterface
    @Override
    public void onMessageFromJs(String message) {
        Log.d(TAG, "收到JS消息: " + message);
        LogUtils.logOperation("WebView", "JS消息", "内容: " + message);
        
        // 如果有需要，可以在这里处理JS发来的消息
        // 例如，更新UI或触发其他操作
    }

    /**
     * 获取通风控制接口
     * @return 通风控制接口
     */
    public WebViewJsVentilationInterface getVentilationInterface() {
        return ventilationInterface;
    }

    /**
     * 获取通风配置接口
     * @return 通风配置接口
     */
    public WebViewJsVentilationConfigInterface getVentilationConfigInterface() {
        return ventilationConfigInterface;
    }

    /**
     * 获取虫情检测接口
     * @return 虫情检测接口
     */
    public WebViewJsPestDetectionInterface getPestDetectionInterface() {
        return pestDetectionInterface;
    }

    /**
     * 获取阈值设置接口
     * @return 阈值设置接口
     */
    public WebViewJsThresholdSettingInterface getThresholdSettingInterface() {
        return thresholdSettingInterface;
    }

    /**
     * 获取报警分析接口
     * @return 报警分析接口
     */
    public WebViewJsAlarmAnalysisInterface getAlarmAnalysisInterface() {
        return alarmAnalysisInterface;
    }
} 